package org.classdef.analyzer.info.fieldtype;

/**
 * 
 * class格式规范中的字段类型
 *
 */
public enum FieldType {
	
	BYTE("B", "BaseType", "byte"),
	CHAR("C", "BaseType", "char"),
	DOUBLE("D", "BaseType", "double"),
	FLOAT("F", "BaseType", "float"),
	INT("I", "BaseType", "int"),
	LONG("J", "BaseType", "long"),
	REFERENCE("L", "ObjectType", "reference"),
	SHORT("S", "BaseType", "short"),
	BOOLEAN("Z", "BaseType", "boolean"),
	ARRAY("[", "ArrayType", "reference");
	
	private String value;
	private String type;
	private String name;
	
	private FieldType(String value, String type, String name){
		this.value = value;
		this.type = type;
		this.name = name;
	}
	
	public String getValue() {
		return value;
	}

	public String getType() {
		return type;
	}

	public String getName() {
		return name;
	}
	
	public static FieldType getFieldType(String value){
		FieldType[] types = values();
		String v = String.valueOf(value.charAt(0));
		for (FieldType type : types) {
			if(type.getValue().equals(v)){
				return type;
			}
		}
		return null;
	}

	public static String getType(String value){
		FieldType[] types = values();
		String v = String.valueOf(value.charAt(0));
		for (FieldType type : types) {
			if(type.getValue().equals(v)){
				if(type.getType().equals("BaseType")){
					return type.getName();
				}else if(type.getType().equals("ObjectType")){
//					return value.substring(1, value.length() - 1).replaceAll("/", ".");
					String[] strs = value.substring(1, value.length() - 1).split("/");
					return strs[strs.length - 1];
				}else if(type.getType().equals("ArrayType")){
					return getType(value.substring(1)) + "[]";
				}
			}
		}
		return "";
	}

}
